package com.kobako.model;

import org.junit.Test;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static com.kobako.WebUtil.isLinux;

/**
 * Created by kobako on 2017/3/19.
 * Just a game
 */
@Service
public class IndexChartDao {
    public String getIndexModel(){
        if(isLinux())return getJsonFromLinux();
        else return getJsonFromWin();
    }

    private String getJsonFromLinux(){
        String path = "/usr/java_project/setting/indexChart.json";
        StringBuilder result = new StringBuilder();
        try {
            Files.readAllLines(Paths.get(path)).forEach(result::append);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    private String getJsonFromWin(){
        StringBuilder result = new StringBuilder();
        String classesPath = Thread.currentThread().getContextClassLoader()
                .getResource("").getPath();
        boolean isLinux = isLinux();
        if(!isLinux)classesPath = classesPath.substring(1);
        classesPath += "static/setting/indexChart.json";

        try {
            Files.readAllLines(Paths.get(classesPath)).forEach(result::append);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

//    @Test
//    public void testGetIndexModel(){
//        System.out.println(getIndexModel());
//    }
}
